Ext.define('AmpleHarvest.objects.metrics.site.cDetailReport',
{
	extend			: 'Ext.panel.Panel',
	alias			: 'widget.ampleharvest-objects-metrics-site-cdetailreport',
	title			: 'LOADING...',
	titleAlign		: 'center',	
	requires		:
	[
			'Ext.LoadMask'
	],
	uses			:
	[
	 		'AmpleHarvest.objects.metrics.util.cMetricsUtils'
	],
	anchor			: '100% 100%',
	autoScroll		: true,
	layout			: 'anchor',
	listeners		:
	{
		loadData	: function(obj_data)
		{
			var obj_process = new Object();
			if(Ext.isArray(obj_data.data))
			{
				str_title = obj_data.data[0].title;
				obj_process = obj_data.data[0];
			} else {
				str_title = obj_data.data.title;
				obj_process = obj_data.data;
			}
			this.setTitle(str_title);
			this.process_response(obj_process);
		},
		
		loadROCReport	: function(obj_data)
		{
			this.m_obj_reportROCObject = Ext.create("AmpleHarvest.objects.metrics.sitesRollup.cList",
					{
						title			: 'ROC report',
						type			: 'threads',
						anchor			: '97% 40%',
						columns: [
						          { header: 'Title', 					dataIndex: 'title', 				flex: 1 },
						          { header: 'Avg Rate of change', 		dataIndex: 'postAvgRateOfChange', 	flex: 1 },
						          { header: 'Max Rate of change', 		dataIndex: 'postMaxRateOfChange', 	flex: 1 },
						          { header: 'Most Recent Delta', 		dataIndex: 'postMostRecentDelta', 	flex: 1 },
						          { header: 'Standard Deviation', 		dataIndex: 'postStandardDeviation', flex: 1 },
						          { header: 'Views', 					dataIndex: 'viewCount', 				flex: 1 },
						          { header: 'Avg Rate of change', 		dataIndex: 'viewAvgRateOfChange', 	flex: 1 },
						          { header: 'Max Rate of change', 		dataIndex: 'viewMaxRateOfChange', 	flex: 1 },
						          { header: 'Standard Deviation', 		dataIndex: 'viewStandardDeviation', flex: 1 }
						      ],
						obj_data		: obj_data.data.data[0].threads.data,
						AHcaller		: this,
						bool_showTbar	: false,
						bool_detailLoad : true,
						report			: false
					});
		}
	},
	initComponent	: function()
	{
		this.util_metrics = Ext.create('AmpleHarvest.objects.metrics.util.cMetricsUtils');
		
		this.m_obj_Details = Ext.create("AmpleHarvest.objects.metrics.site.objects.cDetails");

		this.m_obj_Toolbar = Ext.create('Ext.toolbar.Toolbar', 
				{
					alias : 'widget.ampleharvest-objects-metrics-site-objects-ctoolbar'
				});
		
		
		this.items =
			[
			 	this.m_obj_Details
			 ];
		
		this.callParent();
		
		this.addDocked(this.m_obj_Toolbar, 'top');
		
		this.util_metrics.get_json({restPath	: this.m_str_id}, this)
	},

	process_response	: function(obj_data)
	{
//		console.log('template stuff');
//		console.log(obj_data);
		this.m_obj_Details.obj_template.overwrite(this.m_obj_Details.body, obj_data);
		
		var str_href_container = this.util_metrics.getLink('container', obj_data['link-tree'].link);
		if(str_href_container)
		{
			this.m_obj_btn_Container = Ext.create('Ext.Button',
				{
					text	:'Containing List',
					tooltip	: 'Go to Summary list containing this piece of data',
					handler	: function()
						{
							m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cMenuEvents.C_STR_EVENT_GO_METRIC_SITE_SUMMARY,
									str_href_container
							);
						}
				});
			this.m_obj_Toolbar.add(this.m_obj_btn_Container);
		}
		
		var str_href_parentSite = this.util_metrics.getLink('parentSite', obj_data['link-tree'].link);
		if(str_href_parentSite)
		{
			this.m_obj_btn_ParentSite = Ext.create('Ext.Button',
				{
					text	:'Parent Site',
					tooltip	: 'Go to parent site',
					handler	: function()
						{
							m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_METRICS_SITE_DETAIL,
									str_href_parentSite
							);
						}
				});
			this.m_obj_Toolbar.add(this.m_obj_btn_ParentSite);
		}

		var str_href_parentForum = this.util_metrics.getLink('parentForum', obj_data['link-tree'].link);
		if(str_href_parentForum)
		{
			this.m_obj_btn_ParentForum = Ext.create('Ext.Button',
				{
					text	:'Parent Forum',
					tooltip	: 'Go to parent forum',
					handler	: function()
						{
							m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_METRICS_SITE_DETAIL,
									str_href_parentForum
							);
						}
				});
			this.m_obj_Toolbar.add(this.m_obj_btn_ParentForum);
		}
		
		var str_href_topicModel = this.util_metrics.getLink('topicmodel', obj_data['link-tree'].link);
		if(str_href_topicModel)
		{
			this.m_obj_btn_ParentForum = Ext.create('Ext.Button',
				{
					text	:'Topic Models',
					tooltip	: 'View Topic Models',
					scope	: this,
					handler	: function()
						{
						console.log(obj_data);
							m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_TOPIC_MODEL,
									str_href_topicModel, obj_data.siteId, this.util_metrics.buildLink(obj_data.self.urn)
							);
						}
				});
			this.m_obj_Toolbar.add(this.m_obj_btn_ParentForum);
		}
		
		if(Ext.isObject(obj_data.snapshotPages))
		{
			this.m_obj_btn_snapShotDD = Ext.create("Ext.menu.Menu",
					{
						hideOnClick	: false,
						items		: []
					});
			
			if(!Ext.isArray(obj_data.snapshotPages.pages))
			{
				obj_data.snapshotPages.pages = [obj_data.snapshotPages.pages];
			}
			// TODO: check 'obj_data.snapshotPages.pages' if array, if not set it inside one 
			
			for( var v in obj_data.snapshotPages.pages)
			{
				var int_displayval = parseFloat(v) + 1
				this.m_obj_btn_snapShotDD.add({
		            text  	 : 'Snapshot:'+int_displayval,
		            tooltip  : "Snapshot",
		            iconCls  : "menu-search-file-icon",
		            targetVal: v,
		            handler  : function()
		            {
		            	window.location.href = obj_data.snapshotPages.pages[this.targetVal].link['href'];
		            }
		         });
			}
			
			this.m_obj_Toolbar.add({
				text	: 'Snapshots',
				iconCls	: "menu-search-icon",
				tooltip	: "Snapshots",
				menu	: this.m_obj_btn_snapShotDD

			});
			
		}
		
//		var obj_root_reports = this.util_metrics.getLink('reports', obj_data['link-tree'])
//		console.log(obj_root_reports.urn)
//		
		this.util_metrics.createList(obj_data,
				{
					type	: 'reports',
					title	: '',
					anchor	: '97% 40%',
					multiSelect: true,
					columns	: [
						        { header: 'Reports',  		dataIndex: 'title', 		flex: 1 }
						 ],
					tbar	: true,
					showDefaultTbar	: false,
					tbarOptional	: [ Ext.create("Ext.button.Button",
							{
						text		: 'Open Selected',
//						test		: obj_data['link-tree'],
						iconCls		: "menu-group-add-icon",
						obj_rep_data: this.util_metrics.getLink('reports', obj_data['link-tree'].link),
						handler		: function()
						{
							var arr_selection = this.up('ampleharvest-objects-metrics-sitesrollup-clist').getSelectionModel().getSelection();
							var str_reportPath = '';
							var str_semi = '';
							console.log(this.obj_rep_data);
							for(var x in arr_selection)
							{
								if(x > 0)
								{
									str_semi = ';';
								}
								str_reportPath += str_semi+arr_selection[x].get('reportId');
							}
							console.log(this.obj_rep_data+'/'+str_reportPath);
							m_obj_AmpleHarvest_ViewPort
								.fireEvent(
										AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_METRICS_SITE_REPORT,
										this.obj_rep_data+'/'+str_reportPath
								);
						}
					})
					],
					caller	: this,
					detail	: false,
					report	: true
				});
		
		
		if (this.util_metrics.findByNameValue(obj_data.reports, 'reportId', 'r6')&&this.util_metrics.findByNameValue(obj_data.reports, 'reportId', 'r7'))
		{
			var obj_report = this.util_metrics.findByNameValue(obj_data.reports, 'reportId', 'r6');
			if(Ext.isObject(obj_report))
			{
				var obj_extraParams =  new Object();
				obj_extraParams.restPath = this.util_metrics.getLink('self', obj_report.self) + ';r7';
				obj_extraParams.title = 'Combined ROC Report';
				obj_extraParams.passthrough = {};
				obj_extraParams.eventOverride = 'loadROCReport';
				
				this.util_metrics.get_json(obj_extraParams, this);

			}
		}
		
		this.util_metrics.createList(obj_data,
				{
					type	: 'forums',
					title	: 'Forums',
					anchor	: '97% 40%',
					columns	: [
					          { header: 'Title', 		dataIndex: 'title', 		flex: 1 },
					          { header: 'Posts', 		dataIndex: 'postCount', 		flex: 1 },
					          { header: 'Views', 		dataIndex: 'viewCount', 		flex: 1 },
					          { header: 'Harvested', 	dataIndex: 'harvestDate', 	flex: 1 },
					          { header: 'Last Post', 	dataIndex: 'lastPostDate', 	flex: 1 }
					      ],
					tbar	: false,
					caller	: this,
					detail	: true,
					report	: false
				});
		
		this.util_metrics.createList(obj_data,
				{
					type	: 'threads',
					title	: 'Threads',
					anchor	: '97% 40%',
					columns	: [
						        { header: 'Threads',  	dataIndex: 'title', 		flex: 1 },
						        { header: 'Posts', 		dataIndex: 'postCount', 		flex: 1 },
						        { header: 'Views', 		dataIndex: 'viewCount', 		flex: 1 }
						 ],
					tbar	: false,
					caller	: this,
					detail	: true,
					report	: false
				});

		var str_href_posts = this.util_metrics.getLink('posts', obj_data['link-tree'].link);
		if(Ext.isObject(obj_data.posts)||Ext.isString(str_href_posts))
		{
			this.m_obj_postObject = Ext.create("AmpleHarvest.objects.metrics.sitesRollup.postList",
					{
						title			: 'Posts',
						anchor			: '97% 80%',
					    obj_data		: obj_data.posts.data,
					    str_directPath	: str_href_posts,
						AHcaller		: this
					});
		}
	}
}
);